/* -*- Mode: C -*- */
/******************************************************************************
*
*  Source File Name = retrieve.sqx
*
*  Component Name   = DB2 XML Extender
*
*  Descriptive Name = Test for dxxRetrieveXML
*
*  Copyright = 5622-044 (c) Copyright IBM Corp 1987, 1997, 2001,2002
*              Licensed Materials - Program Property of IBM
*
*  Status = New code
*
*  Function = This client program tests the SQL-to-XML mapping stored
*             procedure of DB2 XML Extender.
*             It takes the arguments from the command line and calls the stored
*             procedure dxxRetrieve.
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlutil.h>
#include <sqlenv.h>
#include "sqlca.h"
#include "dxx.h"
#include "dxxrc.h"

/*******************************************************************
** main
*******************************************************************/
int main(int argc, char *argv[]) {
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
  char    dbname[18];
  char    result_tabname[160];
  char    collectionName[129];
  sqlint32 overrideType= NO_OVERRIDE;
  struct override_t {
	char buf[32672];
  } *override;
  sqlint32 m;
  sqlint32 n;
  sqlint32 errCode;
  char    msgtext[2048];
  short   null_ind = -1;
  short   override_ind = -1; 
  short   n_ind;
  short   errCode_ind;
  short   msgtext_ind;
  short   collectionName_ind;
  short   result_tabname_ind;
  short   m_ind;
  short   overrTypeInd;
EXEC SQL END DECLARE SECTION;
  int     rc = 0, len;
  int     ERROR_CASE = 0;
  char    errorMsg[1024];

  override=NULL;
  /* Process command-line arguments */
  if ((argc != 7) && (argc != 5) && (argc != 4)) {  
    printf("Usage: %s dbname collectionname result_tabname "
           "[max_ndocs|{-o overrideType override}]\n", 
           argv[0]);
    return -1;
  }
  strcpy(dbname,         argv[1]);
  strcpy(collectionName, argv[2]);
  strcpy(result_tabname, argv[3]);
  m = (argc == 5)? atol(argv[4]): 0;
  printf("m:%d\n", m);
  override= (struct override_t *)calloc(1, sizeof(struct override_t));
  if (override == NULL) {
	printf("out of memory\n");
	goto exit;
  }
  if (argc > 4) {
    if (!strcmp(argv[4], "-o")) {
      /* Enable override. */
      overrideType =
        (!strcmp(argv[5],"SQL_OVERRIDE"))? SQL_OVERRIDE: XML_OVERRIDE;
      strcpy(override->buf, argv[6]);
      override_ind = 0;
    } else {
      override->buf[0]='\0';
      overrideType = NO_OVERRIDE;
    }
  }

  /* for error case */
  if (m == -1)
     ERROR_CASE = 1;
  collectionName_ind = 0;
  result_tabname_ind = 0;
  m_ind              = 0;
  overrTypeInd       = 0;

  /* Connect to database */
  fprintf(stdout, "Connecting to database %s\n", dbname); /* @U5 */
  EXEC SQL CONNECT TO :dbname;

  /* Call the stored procedure. */
  if (!ERROR_CASE) {
           EXEC SQL CALL db2xml.dxxRetrieveXML(:collectionName:collectionName_ind,
                                               :result_tabname:result_tabname_ind,
                                               :overrideType:overrTypeInd,
                                               :*override:override_ind,
                                               :m:m_ind,
                                               :n:n_ind,
                                               :errCode:errCode_ind,
                                               :msgtext:msgtext_ind);
  }

  /* Print the output parms. */
  printf("  n=%d:%d\n  errCode=%d:%d\n  msgtext'%s':%d\n",
            n, n_ind, errCode, errCode_ind, msgtext, msgtext_ind);
  if ( SQLCODE >= 0) {      
     if( SQLCODE > 0 ){
        sqlaintp( errorMsg , 1024 , 80 , &sqlca );
	    printf("\n The warning message is \"%s\"  and it is being ignored.\n"
            , errorMsg );
     }                   

     if (errCode < DXX_ERRCODE_OK)
        EXEC SQL ROLLBACK;
     else
        EXEC SQL COMMIT;
  }
  else
  {
         sqlaintp( errorMsg , 1024 , 80 , &sqlca );
         printf("\n The error message is %s \n" , errorMsg );
         EXEC SQL ROLLBACK ;

  }
exit:
  if (override)
    free(override);
  return rc;
}
